using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraBars.ViewInfo;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Menu;
using Microsoft.Practices.CompositeUI.SmartParts;
using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using System.Reflection;
using DevExpress.XtraGrid;

namespace NetRosApp.Infrastructure.Interface.Base.BaseViews
{
    public partial class BaseTraziView : XtraUserControl, IBaseTraziView, ISmartPartInfoProvider
    {
        bool NotModal = true;

        ItemClickEventHandler _onIzvjestaj;
        ItemClickEventHandler _onNovi;
        ItemClickEventHandler _onTrazi;
        ItemClickEventHandler _onClear;
        ItemClickEventHandler _onUgasi;
        String _title = "Genericki trazi";
        String _description = "Genericki trazi Description";


        [Category("SmartPartInfo")]
        public String Title
        {
            set { _title = value; }
            get { return _title;             }
        }

        [Category("SmartPartInfo")]
        public String Description
        {
            set { _description = value; }
            get { return _description; }
        }

        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnIzvjestaj
        {
            add
            {
                _onIzvjestaj += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                _onIzvjestaj -= value;
                barButtonItem1.ItemClick -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnNovi
        {
            add
            {
                _onNovi += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem2.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem2.ItemClick -= value;
                _onNovi -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnTrazi
        {
            add
            {
                _onTrazi += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem3.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem3.ItemClick -= value;
                _onTrazi -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnClear
        {
            add
            {
                _onClear += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem4.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem4.ItemClick -= value;
                _onClear -= value;
            }
        }
        [Category("BarGumbi"), Description("Na pritisak gumba")]
        public event DevExpress.XtraBars.ItemClickEventHandler OnUgasi
        {
            add
            {
                _onUgasi += new DevExpress.XtraBars.ItemClickEventHandler(value);
                barButtonItem5.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(value);
            }
            remove
            {
                barButtonItem5.ItemClick -= value;
                _onUgasi -= value;
            }
        }
            

        public void KopiranjeMenu_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            GridView gv =  (sender as GridView);
            if (gv != null && e.MenuType == GridMenuType.Row &&  gv.IsDataRow(gv.FocusedRowHandle) 
                && !gv.IsEditing)
            {
                GridViewMenu menu = new GridViewMenu(gv);
                e.Allow = true;
                menu.Items.Clear();
                menu.Items.Add(new DXMenuItem("Kopiraj",delegate {Clipboard.SetText(gv.GetFocusedDisplayText());}));
                e.Menu = menu;
            }
        }

        public BaseTraziView()
        {
            InitializeComponent();

        }



        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            const int WM_KEYDOWN = 0x100;
            const int WM_SYSKEYDOWN = 0x104;

            if (msg.Msg == WM_KEYDOWN || msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.F1:
                        HelperUtility.OpenHelp(this.Name.ToString());
                        break;

                    case Keys.F5:
                        if (NotModal & _onIzvjestaj != null)
                        {
                            _onIzvjestaj.Invoke(this, new ItemClickEventArgs(barButtonItem1, null));
                        }
                        break;
                    case Keys.F6:
                        if (_onNovi != null)
                        {
                            _onNovi.Invoke(this, new ItemClickEventArgs(barButtonItem2, null));
                        }
                        break;
                    case Keys.F8:
                        if (_onTrazi != null)
                        {
                            _onTrazi.Invoke(this, new ItemClickEventArgs(barButtonItem3, null));
                        }
                        break;

                    case Keys.F9:
                        if (_onClear != null)
                        {
                            _onClear.Invoke(this, new ItemClickEventArgs(barButtonItem4, null));
                        }
                        break;

                    case Keys.F12:
                        if (_onUgasi != null)
                        {
                            _onUgasi.Invoke(this, new ItemClickEventArgs(barButtonItem5, null));
                        }
                        break;

                    case Keys.F10:
                        Bar mainMenu = barManager1.MainMenu;
                        FieldInfo fi1 = typeof(DevExpress.XtraBars.BarManager).GetField("selectionInfo", BindingFlags.NonPublic | BindingFlags.Instance);
                        FieldInfo fi2 = typeof(DevExpress.XtraBars.Bar).GetField("barControl", BindingFlags.NonPublic | BindingFlags.Instance);
                        DevExpress.XtraBars.Controls.CustomBarControl _cbc = fi2.GetValue(bar2) as DevExpress.XtraBars.Controls.CustomBarControl;
                        BarSelectionInfo _bsi = fi1.GetValue(barManager1) as BarSelectionInfo;
                        if (_bsi != null && _cbc != null)
                        {
                            if (_bsi.ActiveBarControl == null)
                            {
                                _bsi.ActiveBarControl = _cbc;

                                _bsi.MoveLinkSelectionHorizontal(_cbc.GetVisibleLinks(), BarLinkNavigation.First);
                            }
                            else
                            {
                                _bsi.Clear();
                            }
                        }
                        break;
                    case  Keys.Control | Keys.W:

                        Type t = this.GetType();
                        MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Public |
                        BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance);
                        foreach (MemberInfo member in members)
                        {
                            if (member.MemberType == MemberTypes.Field)
                            {
                                FieldInfo fi = t.GetField(member.Name,
                                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                                if (fi.FieldType == typeof(DevExpress.XtraLayout.LayoutControlGroup))
                                {
                                    DevExpress.XtraLayout.LayoutControlGroup tes = (fi.GetValue(this) as DevExpress.XtraLayout.LayoutControlGroup);
                                    if (tes.ExpandButtonVisible == true)
                                    {
                                        tes.Expanded = !tes.Expanded;
                                    }
                                }
                            }
                        }
                        break;
                         case Keys.Control | Keys.Shift | Keys.P:
                                Type t2 = this.GetType();
                                MemberInfo[] members2 = t2.GetMembers(BindingFlags.NonPublic | BindingFlags.Public |
                                BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance);
                                foreach (MemberInfo member in members2)
                                {
                                    if (member.MemberType == MemberTypes.Field)
                                    {
                                        FieldInfo fi = t2.GetField(member.Name,
                                        BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                                        if (fi.FieldType == typeof(DevExpress.XtraGrid.GridControl))
                                        {
                                            DevExpress.XtraGrid.GridControl tes = (fi.GetValue(this) as DevExpress.XtraGrid.GridControl);
                                            if (tes != null)
                                            {
                                                DevExpress.XtraPrinting.PrintingSystem _ps = new DevExpress.XtraPrinting.PrintingSystem();

                                                DevExpress.XtraPrinting.PrintableComponentLink _pcl = new DevExpress.XtraPrinting.PrintableComponentLink();
                                                _pcl.Margins = new System.Drawing.Printing.Margins(36, 36, 40, 40);
                                                _pcl.PaperKind = System.Drawing.Printing.PaperKind.A4;
                                                _pcl.Component = tes;
                                                _pcl.Landscape = true;
                                                _ps.Links.Add(_pcl);
                                                _pcl.CreateDocument();
                                                _pcl.ShowPreviewDialog();
                                            }
                                        }
                                    }
                                }
                                break;

                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected internal virtual void CloseAndReleaseView(IBaseTraziPresenter _presenter)
        {
            _presenter.OnCloseView();
        }
        Rectangle GetColumnBounds(GridColumn column)
        {
          
            if (column == null) return Rectangle.Empty;
            GridViewInfo viewInfo = column.View.GetViewInfo() as GridViewInfo;
            if (viewInfo != null)
            {
                if (viewInfo.ColumnsInfo[column] != null)
                    return viewInfo.ColumnsInfo[column].Bounds;
            }
            return Rectangle.Empty;
        }
        private void gvvwDokumentList_KeyDown(object sender, KeyEventArgs e)
        {

            if ((e.KeyData) == Keys.Apps)
            {
                GridControl gc = sender as GridControl;
                if (gc != null)
                {
                    GridView view = gc.FocusedView as GridView;
                    if (view != null)
                    {
                        GridColumn column = view.FocusedColumn;
                        Rectangle rect = GetColumnBounds(column);
                        if (!rect.IsEmpty)
                        {
                            Point pt = rect.Location;
                            pt.Offset(20, 10);
                            GridViewColumnMenu menu = new GridViewColumnMenu(view);
                            menu.Init(column);
                            menu.Show(pt);
                            e.Handled = true;
                        }
                    }
                }
            }
        }
        protected override  void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Type t = this.GetType();
            MemberInfo[] members = t.GetMembers(BindingFlags.NonPublic | BindingFlags.Public |
            BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance);
            foreach (MemberInfo member in members)
            {
                if (member.MemberType == MemberTypes.Field)
                {
                    FieldInfo fi = t.GetField(member.Name,
                    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                    if (fi.FieldType == typeof(DevExpress.XtraLayout.LayoutControl))
                    {
                        DevExpress.XtraLayout.LayoutControl tes = (fi.GetValue(this) as DevExpress.XtraLayout.LayoutControl);
                        tes.AllowCustomizationMenu = false;
                        tes.OptionsFocus.AllowFocusGroups = false;
                        tes.Padding = new Padding(0);
                        tes.OptionsView.HighlightFocusedItem = true;
                    }
                    if (fi.FieldType == typeof(DevExpress.XtraLayout.LayoutControlGroup))
                    {
                        DevExpress.XtraLayout.LayoutControlGroup tes = (fi.GetValue(this) as DevExpress.XtraLayout.LayoutControlGroup);
                        if (tes != null)
                        {
                            tes.Padding= new DevExpress.XtraLayout.Utils.Padding(0)
                                ; }
                    }
                    if (fi.FieldType == typeof(DevExpress.XtraGrid.GridControl))
                    {
                        DevExpress.XtraGrid.GridControl tes = (fi.GetValue(this) as DevExpress.XtraGrid.GridControl);
                        if (tes != null)
                        {
                            tes.KeyDown += gvvwDokumentList_KeyDown;
                             if (tes.MainView is GridView)
                             {
                                 ((GridView)tes.MainView).PopupMenuShowing += KopiranjeMenu_PopupMenuShowing;
                             }
                            //tes.Leave += delegate(object sender, EventArgs ex)
                            //                     {

                            //                         if (tes != null && tes.FocusedView as GridView != null)
                            //                             if (((GridView)tes.FocusedView).FocusedRowHandle == GridControl.NewItemRowHandle)
                            //                             {
                            //                                 ((GridView)tes.FocusedView).HideEditor();
                            //                                 ((GridView)tes.FocusedView).CancelUpdateCurrentRow();
                            //                             }
                            //                     };
                            //DevExpress.XtraPrinting.PrintingSystem _ps = new DevExpress.XtraPrinting.PrintingSystem();

                            //DevExpress.XtraPrinting.PrintableComponentLink _pcl = new DevExpress.XtraPrinting.PrintableComponentLink();
                            //_pcl.Margins = new System.Drawing.Printing.Margins(36, 36, 40, 40);
                            //_pcl.PaperKind = System.Drawing.Printing.PaperKind.A4;
                            //_pcl.Component = tes;
                            //_pcl.Landscape = true;
                            //_ps.Links.Add(_pcl);
                            //_pcl.CreateDocument();
                            //_pcl.ShowPreviewDialog();
                        }
                    }
                    if (fi.FieldType == typeof(SpinEdit))
                    {
                        SpinEdit tes = (fi.GetValue(this) as SpinEdit);
                        if (tes != null)
                        {
                            if (tes.Properties.Buttons.Count == 1)
                            {
                                tes.Properties.Buttons[0].Visible = false;
                            }
                            tes.Spin += delegate(object send, DevExpress.XtraEditors.Controls.SpinEventArgs es)
                                            {
                                                es.Handled = true;
                                            };
                        }
                    }
                    if (fi.FieldType == typeof(DevExpress.XtraEditors.Repository.RepositoryItemSpinEdit))
                    {
                        DevExpress.XtraEditors.Repository.RepositoryItemSpinEdit tes = (fi.GetValue(this) as DevExpress.XtraEditors.Repository.RepositoryItemSpinEdit);
                        if (tes != null)
                        {
                            if (tes.Buttons.Count == 1)
                            {
                                tes.Buttons[0].Visible = false;
                            }
                            tes.Spin += delegate(object send, DevExpress.XtraEditors.Controls.SpinEventArgs es)
                                            {
                                                es.Handled = true;
                                            };
                        }
                    }
                }

            }
            if ((Parent as Form) != null)
            {
                NotModal = false;
                if ((Parent as Form) != null)
                {
                    barButtonItem1.Enabled = false;
                    this.Dock = DockStyle.Fill;
                }
            }
        }

         public  ISmartPartInfo GetSmartPartInfo(Type smartPartInfoType)
        {
            ISmartPartInfo result = Activator.CreateInstance(smartPartInfoType) as ISmartPartInfo;
            result.Title = _title;
            result.Description = _description;
            return result;
        } 

        public object   SmartPartView {
            get { return this; }
            }
     public bool IsFocused()
        {
            return this.ContainsFocus;
        }

        protected internal virtual void   OpenEntityEditor(IBaseTraziPresenter _presenter,object Entity,Type EditorClass)
        {
            if (Entity != null)
            {

                if (!NotModal)
                {

                    _presenter.OpenEditorNewOrEditinParent(Entity);
                    try
                    {
                        (Parent as Form).Close();
                    }
                    catch { }
                }
                else
                {
                    if (EditorClass != null)
                    _presenter.OpenEditorNewOrEditinTab(Entity, EditorClass);
                }
            }
        }

        protected internal virtual void OpenEntityEditorNew(IBaseTraziPresenter _presenter, Type EditorClass)
        {
            if (!NotModal)
            {
                _presenter.OpenEditorNewModal(EditorClass);
            }
            else
            {
                _presenter.OpenEditorNewOrEditinTab(null, EditorClass);
            }

        }

        public void GridView_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                OpenEditor();
            }

        }

        public void GridControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.GridControl tmp = sender as DevExpress.XtraGrid.GridControl;
            GridHitInfo hi = tmp.FocusedView.CalcHitInfo(new System.Drawing.Point(e.X, e.Y)) as GridHitInfo;
            if (hi.InRow)
            {
                OpenEditor();
            }

        }

        protected virtual void OpenEditor()
        {
            MessageBox.Show("OpenEditor");
        }
        
        protected internal void PostaviStyleConditionZaGrid(GridView gridView1,int Storno)
        {
            PostaviStyleConditionZaGrid(gridView1, Storno, System.Drawing.Color.FromArgb(180, ((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192))))));
        }

        protected internal virtual  void PostaviStyleConditionZaGrid(GridView gridView1, int Storno,Color BojaStorno)
        {
            DevExpress.XtraGrid.StyleFormatCondition styleFormatCondition1 = new DevExpress.XtraGrid.StyleFormatCondition();
            DevExpress.XtraGrid.StyleFormatCondition styleFormatCondition2 = new DevExpress.XtraGrid.StyleFormatCondition();
            gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            styleFormatCondition1.Appearance.BackColor = BojaStorno;
            styleFormatCondition1.Appearance.Options.UseBackColor = true;
            styleFormatCondition1.ApplyToRow = true;
            styleFormatCondition1.Column = gridView1.Columns["IDDokumentTip"];
            styleFormatCondition1.Condition = DevExpress.XtraGrid.FormatConditionEnum.Equal;
            styleFormatCondition1.Value1 = Storno;
            styleFormatCondition2.Appearance.BackColor = System.Drawing.Color.FromArgb(180, ((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            styleFormatCondition2.Appearance.Options.UseBackColor = true;
            styleFormatCondition2.ApplyToRow = true;
            styleFormatCondition2.Column = gridView1.Columns["IDStatus"];
            styleFormatCondition2.Condition = DevExpress.XtraGrid.FormatConditionEnum.Equal;
            styleFormatCondition2.Value1 = 0;
            gridView1.FormatConditions.AddRange(new DevExpress.XtraGrid.StyleFormatCondition[] {
            styleFormatCondition1,
            styleFormatCondition2});
            //gridView1.Appearance.FocusedRow.BackColor = System.Drawing.Color.FromArgb(20, 0, 0, 240);
        }

        protected internal virtual void PostaviStyleConditionZaGrid(GridView gridView1)
        {
            DevExpress.XtraGrid.StyleFormatCondition styleFormatCondition2 = new DevExpress.XtraGrid.StyleFormatCondition();
            gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            styleFormatCondition2.Appearance.BackColor = System.Drawing.Color.FromArgb(180, ((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            styleFormatCondition2.Appearance.Options.UseBackColor = true;
            styleFormatCondition2.ApplyToRow = true;
            styleFormatCondition2.Column = gridView1.Columns["IDStatus"];
            styleFormatCondition2.Condition = DevExpress.XtraGrid.FormatConditionEnum.Equal;
            styleFormatCondition2.Value1 = 0;
            gridView1.FormatConditions.AddRange(new DevExpress.XtraGrid.StyleFormatCondition[] {
            styleFormatCondition2});
            //gridView1.Appearance.FocusedRow.BackColor = System.Drawing.Color.FromArgb(20, 0, 0, 240);
        }

    }
}
